package com.tbit.common.entity.view;




import com.tbit.common.constant.HttpStatus;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 通用返回响应结果对象
 *
 * @Author:BUGTian
 * @DATE: 2022/05/10
 */
public class Result<T> implements Serializable {

    /**
     * 响应结果的信息
     *
     * @mock 操作成功
     */
    private String msg;

    /**
     * 响应结果的信息状态码,20X表示成功,40X表示错误，50X表示服务端异常
     *
     * @mock 200
     */
    private Integer code;

    /**
     * 响应结果的逻辑数据
     */
    private T data;

    public Result() {}

    private Result(String msg, Integer code, T data) {
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    /**
     * 成功 返回自定义响应码
     *
     * @param msg  信息
     * @param data 数据
     * @param code 响应码
     * @param <T>
     * @return
     */
    public static <T> Result<T> successCode(String msg, T data, Integer code) {
        return new Result<T>(msg, code, data);
    }

    /**
     * 成功 并不返回数据
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success() {
        return new Result<>("操作成功", HttpStatus.SUCCESS, null);
    }

    /**
     * 成功 并响应数据和信息
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(String msg, T data) {
        return new Result<T>(msg, HttpStatus.SUCCESS, data);
    }

    /**
     * 成功 并返回数据
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>("操作成功", HttpStatus.SUCCESS, data);
    }

    public static <T> Result<PageResult<T>> emptyPage(Integer page, Integer size) {
        return success(new PageResult<>(page, size, 0L, 0, Collections.emptyList()));
    }

    public static <T> Result<PageResult<T>> successPage(Integer page, Integer size, Integer dataCount,
                                                        List<T> resultList) {
        return success(new PageResult<>(page, size, dataCount.longValue(),
                dataCount % size == 0 ? (int) (dataCount / size) : (int) (dataCount / size) + 1,
                resultList));
    }

    /**
     * 系统错误 不返回数据
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> error() {
        return new Result<T>("操作失败", HttpStatus.ERROR, null);
    }

    /**
     * 系统错误 并返回逻辑数据
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(T data) {
        return new Result<T>("操作失败", HttpStatus.ERROR, data);
    }

    /**
     * 系统错误 不返回数据
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> error(String msg) {
        return new Result<T>(msg, HttpStatus.ERROR, null);
    }

    /**
     * 自定义错误和状态返回
     *
     * @param message
     * @param code
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> errorMessage(String message, Integer code, T data) {
        return new Result<T>(message, code, data);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public boolean unSuccess() {
        return this.code != HttpStatus.SUCCESS;
    }
    /**
     * 状态码是否为成功
     */
    public boolean isSuccess() {
        return this.code == HttpStatus.SUCCESS;
    }

}
